$m(function(args){
	var $this = this.element, that = this;
	var callback = args.callback;
	var tree = $this.find(".app-tree");
	var parentId = args.parentId;
	
	var initDialog = function(){
		$this.dialog({
			title: getI18n("app_add_achievement"),
			height: 500,
			width: 600,
			resizable:false,
			modal : true,
			open:function(){
				initLayout();
				initEvent();
				initComponent();
				loadWkList();
				selectAppRender();
				//处理外网UAT反馈添加成果弹出框内容显示不全问题
				$this.find(".container-layout").css("height", "100%");
				$this.find(".ui-layout-pane-west").css("height", "100%");
				$this.find(".ui-layout-pane-center").css("height", "100%");
			},
			close:function(){
				$this.dialog('destroy').remove();
			},
			buttons:[{
				type : "ok",
				click : function(e) {
					confirmHandle() ? $this.dialog("close") : "";
				}
			},{
				type : "cancel",
				click : function(e) {
					$this.dialog('close');
				}
			}]
		});
	}
	
	var initLayout = function(){
		$this.find(".container-layout").layout({
			west : {
				resizable : false,
				size : 220,
				spacing_open : 0,
				closable : false
			}
		});
	}
	
	var initComponent = function(){
		// 空间搜索
		$this.find(".left-container .search-bar").searchbar({
			width: '100%',
			isEnter: true,
			placeholder: getI18n("please_input_name"),
			onSearch: function(text){
				loadWkList({name: text});
			}
		});
		// 成果搜索
		$this.find(".right-container .search-bar").searchbar({
			width: '100%',
			isEnter: true,
			placeholder: getI18n("please_input_name"),
			onSearch: function(text){
				loadAppTree({
					name: text
				});
			}
		});
	}
	
	var initEvent = function(){
		// 空间列表单击事件
		$this.find(".wk-list").on("click", "li", function(event){
			var wkData = $(this).data("data");
			if (wkData.id == "more") {
				return
			}
			$(this).siblings().removeClass("selected");
			$(this).addClass("selected");
			$this.find(".right-container .search-bar").searchbar("clear");
			loadAppTree({
				workspaceId: wkData.id,
				name: ""
			});
		});
	}
	
	var loadAppTree = function(param){
		var p = $.extend(this.appTreeParam, param);
		if(!p.workspaceId) {
			return ;
		}
		appAjax("/api/ai/app/achievement/tree-uncategory", p, function(data){
			renderAppTree(data);
			selectAppRender();
		});
	}
	
	var renderAppTree = function(treeData){
		if (tree.data("tempo-gtree")) {
			tree.gtree("destroy");
		}
		tree.gtree({
			//显示灰度服务挂载的同步服务
			idKey:"uuid",
			data: treeData,
			isRenderCheckbox:function(nodeData){
				if(nodeData.type == "gray"){
					return nodeData.type == "gray";
				}
				return nodeData.type == "flow" || nodeData.type == "plflow";
			},
			onCheckboxChange:function(nodeData, isChecked, event){
				selectAppRender();
			},
			getItemIcon:function(nodeData){
				if(nodeData.type == 'task') {
					return 'tempo-ai-api-icon-tree-16 tempo-api-icon-tree-16-position-49';
				}
			},
			getFolderCloseIcon:function(nodeData){
				if(nodeData.fileType === "ysfw"){
					return "tempo-ai-api-icon-tree-16 tempo-api-icon-tree-16-position-ysfw";
				}
				if (nodeData.type === "gray" || nodeData.fileType === "hdfw") {
					return "tempo-ai-api-icon-tree-16 tempo-api-icon-tree-16-position-gray";
				}else if(nodeData.type === "sync"){
					return "tempo-ai-api-icon-tree-16 tempo-api-icon-tree-16-position-sync";
				}
				if (nodeData.type == "flow" || nodeData.type == "plflow") {
					if (nodeData.fileType == "stream") {
						return 'tempo-ai-api-icon-tree-16 tempo-api-icon-tree-16-position-stream';
					}else if ( nodeData.fileType == "sync") {
						return 'tempo-ai-api-icon-tree-16 tempo-api-icon-tree-16-position-sync';
					}else if (nodeData.fileType == "async"){
						return 'tempo-ai-api-icon-tree-16 tempo-api-icon-tree-16-position-async';
					}
				}else if (nodeData.type == "THEME") {
					return "tempo-api-icon-tree-16  tempo-api-icon-tree-16-position-10";
				}
				return "tempo-ui-icon-api-folder-close";
			},
			getFolderOpenIcon:function(nodeData){
				if(nodeData.fileType === "ysfw"){
					return "tempo-ai-api-icon-tree-16 tempo-api-icon-tree-16-position-ysfw";
				}
				if (nodeData.type === "gray" || nodeData.fileType === "hdfw") {
					return "tempo-ai-api-icon-tree-16 tempo-api-icon-tree-16-position-gray";
				}else if(nodeData.type === "sync"){
					return "tempo-ai-api-icon-tree-16 tempo-api-icon-tree-16-position-sync";
				}
				if (nodeData.type == "flow" || nodeData.type == "plflow") {
					if (nodeData.fileType == "stream") {
						return 'tempo-ai-api-icon-tree-16 tempo-api-icon-tree-16-position-stream';
					}else if ( nodeData.fileType == "sync") {
						return 'tempo-ai-api-icon-tree-16 tempo-api-icon-tree-16-position-sync';
					}else if (nodeData.fileType == "async"){
						return 'tempo-ai-api-icon-tree-16 tempo-api-icon-tree-16-position-async';
					}
				}else if (nodeData.type == "THEME") {
					return "tempo-api-icon-tree-16 tempo-api-icon-16-tree-position-9";
				}
				return "tempo-ui-icon-api-folder-open";
			},
			isLeafNode:function(nodeData){
				if(nodeData.type == "task") {
					return true;
				}else {
					return false;
				}
			}
		});
	}
	
	var selectAppRender = function(){
		var text = "已选中@@个成果";
		$this.find(".right-container .footer").html(TempoUtils.replaceArray(text, "<span>" + getCheckedApp().length + "</span>"));
	}
	
	var loadWkList = function(param){
		var rows = 200;
		appAjax("/api/ai/app/list-pub-workspaces", $.extend(param, {rows: rows, page: 1}), function(data){
			renderWkList(data.rows, data.records > rows);
			var text = "共@@个空间";
			$this.find(".left-container .footer").html(TempoUtils.replaceArray(text, "<span>" + data.records + "</span>"));
		});
	}
	
	var renderWkList = function(wkDatas, isMore){
		var wkUl = $this.find(".wk-list").empty();
		var ul = $("<ul>"), li;
		for (var i = 0, len = wkDatas.length; i < len; i++) {
			li = $("<li>").data("data", wkDatas[i]);
			$("<span>").addClass("icon tempo-api-icon-16 tempo-api-icon-16-position-51").appendTo(li);
			$("<span>").addClass("label").text(wkDatas[i].name).attr("title", wkDatas[i].name).appendTo(li);
			ul.append(li);
		}
		if (isMore) {
			$("<li class='more'>").data("data", {id : "more", name: getI18n("app_more_wk_please_search")})
			.append($("<span>").addClass("line"))
			.append($("<span>").addClass("line-label").text(getI18n("app_more_wk_please_search")))
			.appendTo(ul);
		}
		wkUl.append(ul.children());
	}
	
	var appAjax = function(url, param, callback, type, isProcess){
		var that = this;
		var process;
		if (isProcess) {
			process = $("<div/>").tempoprogressbar();
		}
		$.ajax({
			url: contextPath + url,
			data: param,
			type: type || 'post',
			complete: function(XMLHttpRequest, textStatus){
				process ? process.tempoprogressbar("destroy") : "";
			},
			success: function(resultData){
				if (resultData.success) {
					callback.call(that, resultData.data);
				} else {
					$("<div/>").alert({
						width: 500,
						height: 197,
						msg: resultData.message
					});
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				$("<div/>").alert({
					width: 500,
					height: 197,
					msg: "Error!"
				});
			}
		});
	}
	
	var getCheckedApp = function(){
		if (!tree.data("tempo-gtree")) {
			return [];
		}
		return tree.gtree("getChecked");
	}
	
	var confirmHandle = function(){
		var apps = getCheckedApp();
		if (apps.length == 0) {
			$("<div/>").alert({
				width: 500,
				height: 197,
				msg: "至少选择一个成果"
			});
			return false;
		}
		var ids = apps.map(function(e){ return e.id });
		var progressbar = $("<div/>").tempoprogressbar();
		$.post(contextPath + '/api/ai/app/category/add', {parentId: parentId, id: ids.join(',')}, function(result) {
			progressbar.tempoprogressbar("destroy");
			callback(parentId, apps, result);
		});
		return true;
	}
	
	var getI18n = function(code){
		if (!that.i18n) {
			that.i18n = $("#tempo-app-index-i18n");
		}
		return that.i18n.attr("_" + code);
	}
	
	var init = function(){
		this.appTreeParam = {};
		initDialog();
	}
	
	init();
	
}); 